#pragma managed

#include "StdAfx.h"
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net;

String^ GetData()
{
	String ^ url="http://www.ip.cn/getip.php?action=getip";
	HttpWebRequest^ webRequest;
	HttpWebResponse^ webResponse;

	try
	{
		webRequest = (HttpWebRequest^)WebRequest::Create(url);
		webResponse = (HttpWebResponse^)webRequest->GetResponse();

		if (webResponse->ContentLength > 0)
		{
			int len = (int)webResponse->ContentLength;
			array<byte>^ data = gcnew array<byte>(len);
			Stream^ stream = webResponse->GetResponseStream();
			while (len > 0)
			{
				len -= stream->Read(data, data.Length - len, len);
			}
			stream->Close();
			webResponse->Close();
			return Encoding::ASCII->GetString(data);
		}
		else
		{
			StreamReader^ streamReader = gcnew StreamReader(webResponse->GetResponseStream());
			String^ result = streamReader->ReadToEnd();
			streamReader->Close();
			webResponse->Close();
			return result;
		}
	}
	catch (Exception^ e)
	{
		return String::Empty;
	}
	finally
	{
		if (webResponse != nullptr)
		{
			webResponse->Close();
		}
	}
}